<%@page import="javax.sql.rowset.WebRowSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.security.MessageDigest" %>
<%@ page import="java.security.NoSuchAlgorithmException" %>
<%@ page import="java.math.BigInteger" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.UnsupportedEncodingException" %>
<%
//验证用户身份
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
String pwd = request.getParameter("password");
// 定义加密后的密码变量
String encryptedPassword = "";

try {
    // 创建MessageDigest实例，并指定加密算法为MD5
    MessageDigest md = MessageDigest.getInstance("MD5");
    
    // 更新加密内容
    md.update(pwd.getBytes());
    
    // 获取加密后的字节数组
    byte[] digest = md.digest();
    
    // 将字节数组转换为16进制字符串
    encryptedPassword = new BigInteger(1, digest).toString(16);
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

String retpage = "customerlogin.jsp";
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mealdb?user=root&password=1230321");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM customers WHERE name='"+name+"'");
if(rs.next()){
	//找到记录
	String dbpwd = rs.getString("pwd");
	String encodedCustomerName ="";
	if(dbpwd.equals(encryptedPassword)){
		//pass
		 try {
       // 对customerName进行URL编码，确保它是ASCII编码
      encodedCustomerName = URLEncoder.encode(name, "UTF-8");

   } catch (UnsupportedEncodingException e) {
       // 处理编码错误
       e.printStackTrace();
   }
		retpage = "../customer/customerIndex.jsp?customer_name="+encodedCustomerName;
		
		//记录session
		//s——solder
		//session.setAttribute("sname", name);
		//session.setAttribute("spwd", dbpwd);
	}
}
rs.close();
response.sendRedirect(retpage);
%>